package com.un.ebs.inventory.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.inventory.domain.Warehouse;
import com.un.ebs.inventory.dto.WarehouseAddDto;
import com.un.ebs.inventory.dto.WarehouseUpdateDto;
import com.un.ebs.inventory.service.WarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 仓库 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2020-05-24
 */
@RestController
@RequestMapping("/warehouse")
@Api(value = "warehouse", tags = "")
public class WarehouseController {

    @Autowired
    private WarehouseService service;

    @PostMapping("add")
    @ApiOperation(value = "添加", notes = "添加")
    public WebResult save(@RequestBody @Validated WarehouseUpdateDto req) {

        service.update(req);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "修改", notes = "修改")
    public WebResult update(@RequestBody @Validated WarehouseUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "详情", notes = "根据主键获取单个 Warehouse")
    public WebResult getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("listPage")
    @ApiOperation(value = "查询，带分页", notes = "")
    public WebResult<IPage<Warehouse>> list(@RequestParam(required = false) String searchText, @RequestParam(required = false) String parentId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.query(searchText, parentId, pageSize, pageIndex));
    }

    @GetMapping("list")
    @ApiOperation(value = "查询", notes = "")
    public WebResult<List<Warehouse>> listAll(@RequestParam(required = false) String searchText, @RequestParam(required = false) String parentId) {
        return WebUtils.success(service.query(searchText, parentId, 9999, 1).getRecords());
    }

    @GetMapping("listMatierial")
    @ApiOperation(value = "查询，物料库位", notes = "")
    public WebResult<List<Warehouse>> listMatierial() {
        return WebUtils.success(service.queryMatierial());
    }

    @GetMapping("listProduct")
    @ApiOperation(value = "查询，成品库位", notes = "")
    public WebResult<List<Warehouse>> listProduct() {
        return WebUtils.success(service.queryProduct());
    }

    @PostMapping("changeParent")
    @ApiOperation(value = "变更父节点", notes = "修改 Warehouse 的 parentId 属性")
    public WebResult changeParent(@RequestParam String id, @RequestParam String parentId) {
        service.changeParent(id, parentId);
        return WebUtils.success();
    }

    @GetMapping("/child/{parentId}")
    @ApiOperation(value = "查询子节点", notes = "查询指定 parentId 属性的 Warehouse")
    public WebResult listChild(@PathVariable("parentId") String parentId) {
        return WebUtils.success(service.queryByParentId(parentId));
    }

    @GetMapping("/childPage/{parentId}/{pageSize}/{pageIndex}")
    @ApiOperation(value = "分页查询", notes = "查询指定 parentId 属性的 Warehouse")
    public WebResult listPageChild(@PathVariable("parentId") String parentId, @PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize) {
        return WebUtils.success(service.queryPageByParentId(parentId, pageSize, pageIndex));
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除", notes = "根据主键删除单个 Warehouse")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

}